package com.se2.antiqueexhibition.Controller;

import com.se2.antiqueexhibition.Service.AdminService;
import com.se2.antiqueexhibition.pojo.Admin;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * 管理员登录
     */
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam String username, 
                                     @RequestParam String password, 
                                     HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        Admin admin = adminService.login(username, password);
        if (admin != null) {
            session.setAttribute("admin", admin);
            result.put("success", true);
            result.put("message", "登录成功");
            result.put("data", admin);
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    /**
     * 管理员登出
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/admin/login";
    }

    /**
     * 添加管理员
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addAdmin(@RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();
        if (adminService.addAdmin(admin)) {
            result.put("success", true);
            result.put("message", "添加成功");
        } else {
            result.put("success", false);
            result.put("message", "添加失败，用户名或手机号已存在");
        }
        return result;
    }

    /**
     * 获取管理员列表
     */
    @GetMapping("/list")
    public String getAdminList(ModelMap model) {
        List<Admin> adminList = adminService.getAllAdmins();
        model.put("adminList", adminList);
        return "admin/list";
    }

    /**
     * 获取所有管理员（API）
     */
    @GetMapping("/all")
    @ResponseBody
    public Map<String, Object> getAllAdmins() {
        Map<String, Object> result = new HashMap<>();
        List<Admin> adminList = adminService.getAllAdmins();
        result.put("success", true);
        result.put("data", adminList);
        return result;
    }

    /**
     * 根据ID获取管理员
     */
    @GetMapping("/{id}")
    @ResponseBody
    public Map<String, Object> getAdminById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        Admin admin = adminService.getAdminById(id);
        if (admin != null) {
            result.put("success", true);
            result.put("data", admin);
        } else {
            result.put("success", false);
            result.put("message", "管理员不存在");
        }
        return result;
    }

    /**
     * 更新管理员信息
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> updateAdmin(@RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();
        if (adminService.updateAdmin(admin)) {
            result.put("success", true);
            result.put("message", "更新成功");
        } else {
            result.put("success", false);
            result.put("message", "更新失败");
        }
        return result;
    }

    /**
     * 更新管理员密码
     */
    @PostMapping("/updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(@RequestParam Integer id,
                                              @RequestParam String oldPassword,
                                              @RequestParam String newPassword) {
        Map<String, Object> result = new HashMap<>();
        if (adminService.updatePassword(id, oldPassword, newPassword)) {
            result.put("success", true);
            result.put("message", "密码修改成功");
        } else {
            result.put("success", false);
            result.put("message", "密码修改失败，请检查原密码是否正确");
        }
        return result;
    }

    /**
     * 更新管理员状态
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(@RequestParam Integer id, 
                                            @RequestParam Integer status) {
        Map<String, Object> result = new HashMap<>();
        if (adminService.updateStatus(id, status)) {
            result.put("success", true);
            result.put("message", "状态更新成功");
        } else {
            result.put("success", false);
            result.put("message", "状态更新失败");
        }
        return result;
    }

    /**
     * 删除管理员
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> deleteAdmin(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        if (adminService.deleteAdmin(id)) {
            result.put("success", true);
            result.put("message", "删除成功");
        } else {
            result.put("success", false);
            result.put("message", "删除失败");
        }
        return result;
    }
}
